﻿namespace Home.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration;
    using System.Linq;
    using System.Web;
    using Home.Data;

    public class ServiceDbContext : EntityFrameworkDbContext
    {
        static ServiceDbContext()
        {
            Database.SetInitializer<ServiceDbContext>(null);
        }

        public ServiceDbContext(bool enableTransaction = false)
            : base("Name=DefaultConnection", enableTransaction)
        {
        }

        public DbSet<OptimizationComment> Comments { get; set; }

        public DbSet<OptimizationVisit> Visits { get; set; }

        public DbSet<WebsiteSynchronization> Synchronizations { get; set; }

        protected override void OnModelCreating(DbModelBuilder builder)
        {
            {
                var entity = CreateWebsiteEntity<OptimizationComment>(builder, "tc_optimization_visitor_comment");

                entity.Property(m => m.VisitorId)
                      .HasColumnName("visitor_id")
                      .IsRequired();

                entity.Property(m => m.RegisteredUserId)
                      .HasColumnName("registered_user_id")
                      .IsOptional();

                entity.Property(m => m.CategoryKey)
                      .HasColumnName("category_key")
                      .IsRequired()
                      .HasMaxLength(2048)
                      .IsUnicode(false)
                      .IsVariableLength();

                entity.Property(m => m.CategoryValue)
                      .HasColumnName("category_value")
                      .IsOptional()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.ParentId)
                      .HasColumnName("parent_id")
                      .IsOptional();

                entity.Property(m => m.Content)
                      .HasColumnName("content")
                      .IsOptional()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Score)
                      .HasColumnName("score")
                      .IsOptional();
            }

            {
                var entity = CreateWebsiteEntity<OptimizationVisit>(builder, "tc_optimization_visitor_visit");

                entity.Property(m => m.VisitorId)
                      .HasColumnName("visitor_id")
                      .IsRequired();

                entity.Property(m => m.RegisteredUserId)
                      .HasColumnName("registered_user_id")
                      .IsOptional();

                entity.Property(m => m.Category)
                      .HasColumnName("category")
                      .IsRequired()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.CategoryDetail)
                      .HasColumnName("category_detail")
                      .IsOptional()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Url)
                      .HasColumnName("url")
                      .IsRequired()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Referer)
                      .HasColumnName("referer")
                      .IsOptional()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Agent)
                      .HasColumnName("agent")
                      .IsOptional()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Geolocation)
                      .HasColumnName("geolocation")
                      .IsOptional()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Language)
                      .HasColumnName("language")
                      .IsOptional()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Platform)
                      .HasColumnName("platform")
                      .IsOptional()
                      .HasMaxLength(2048)
                      .IsUnicode(true)
                      .IsVariableLength();
            }

            {
                var entity = builder.CreateEntity<WebsiteSynchronization>("tc_website_sync");

                entity.Property(m => m.Type)
                      .HasColumnName("type")
                      .IsRequired();

                entity.Property(m => m.Status)
                      .HasColumnName("status")
                      .IsRequired();

                entity.Property(m => m.Time)
                      .HasColumnName("time")
                      .IsRequired();
            }
        }

        private static EntityTypeConfiguration<TEntityType> CreateWebsiteEntity<TEntityType>(DbModelBuilder builder, string tableName)
            where TEntityType : WebsiteEntity
        {
            var entity = builder.CreateEntity<TEntityType>(tableName);

            entity.Property(m => m.WebsiteId)
                  .HasColumnName("website_id")
                  .IsRequired();

            entity.Property(m => m.WebsiteCreated)
                  .HasColumnName("website_created")
                  .IsRequired();

            entity.Property(m => m.WebsiteCreatedBy)
                  .HasColumnName("website_created_by")
                  .IsRequired()
                  .HasMaxLength(64)
                  .IsUnicode(true)
                  .IsVariableLength();

            entity.Property(m => m.WebsiteModified)
                  .HasColumnName("website_modified")
                  .IsRequired();

            entity.Property(m => m.WebsiteModifiedBy)
                  .HasColumnName("website_modified_by")
                  .IsRequired()
                  .HasMaxLength(64)
                  .IsUnicode(true)
                  .IsVariableLength();

            return entity;
        }
    }
}